<?php

class Center_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function getById($id) {
        $this->db->select("*");
        $this->db->from('center');
        $this->db->where('center.id_center', $id);
        $this->db->join('users', 'users.id_user = center.id_manager');
         $this->db->join('location','location.id_location = center.id_location');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return NULL;
        }
    }

    /* Lấy danh sách hình ảnh của trung tâm */

    function getListImages($id_center) {
        $this->db->select("*");
        $this->db->from('center');
        $this->db->where('center.id_center', $id_center);
        $this->db->join('image_center', 'image_center.id_center = center.id_center');
        $query = $this->db->get();
        $return = array();
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    
    function getImage($id_center) {
        $this->db->select("*");
        $this->db->from('image_center');
        $this->db->where('image_center.id_center', $id_center);
        $this->db->limit(1);
        $query = $this->db->get();
        $return = array();
        if ($query->num_rows > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }

    function get_type() {
        $data = $this->db->get('type_center');
        return $data->result_array();
    }

    function get_list($type, $sort, $orderBy, $limit, $offset) {
        $this->db->get('center');
        $this->db->join('location', 'location.id_location = center.id_location');
        $this->db->select('id_center, name,discription, rating, view, id_type_center, num, street, ward, district');
        if ($type != null) {
            $this->db->where('id_type_center', $type);
        }
        $this->db->order_by($sort, $orderBy);
        if (!$limit) {
            return $this->db->get('center')->result_array();
        }
        $this->db->limit($limit, $offset);

        $data = $this->db->get('center');
        return $data->result_array();
    }
    //    lay 4 trung tam 
    function getCenters()
    {
        $this->db->select("*");
        $this->db->from('center');
        $this->db->join('location', 'location.id_location = center.id_location');
        //$this->db->join('image_center', 'image_center.id_center = center.id_center');
        $this->db->order_by('create_date','DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows > 0){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
    // tăng số lượng view
    function increate_view($id,$current_view) {
        $this->db->update('center', array('view'=>$current_view+1), array('id_center'=>$id));
    }
    // lấy 4 trung tâm có số lượng view lớn nhất
    function getTopViewCenter()
    {
        $this->db->select('center.name, location.street, location.ward, location.district, center.id_center, center.discription, center.view');
        $this->db->from('center');
        $this->db->join('location', 'location.id_location = center.id_location');
        $this->db->order_by('view', 'DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows > 0){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }

}
